package com.example.tsbs.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.tsbs.config.ImgFileUtil;
import com.example.tsbs.entity.BP;
import com.example.tsbs.entity.BS;
import com.example.tsbs.entity.MZ;
import com.example.tsbs.entity.Task;
import com.example.tsbs.mapper.SystemInfoMapper;
import com.example.tsbs.mapper.TaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Arrays;
import java.util.Objects;

@Slf4j
public class UseUtil {

    /**
     * 执行exeProcess命令
     * @param directory 进程工作目录
     * @param commands Process命令
     * @return
     */
    public static String exeProcessCommand(String directory, String[] commands) {
        String result = "";

        ProcessBuilder processBuilder = new ProcessBuilder();

        // 设置进程命令行参数
        processBuilder.command(commands);
        // 设置进程工作目录
        processBuilder.directory(new java.io.File(directory));
        processBuilder.redirectErrorStream(true);

        StringBuilder readOut = new StringBuilder();
        String lastLine = "";
        try {
            log.info("进程命令开始运行,运行参数：" + Arrays.toString(commands));

            Process process = processBuilder.start();

            // 读取标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                lastLine = line;
                readOut.append(line).append("  ");
            }

            // 等待程序执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            log.info("进程命令运行完毕，" + "退出值为: " + exitValue);

            if (exitValue == 0) {
                result = "成功";
            } else {
                result = "进程运行失败，命令详情："+commands + "；标准输出流:"+lastLine;
                log.info("进程运行失败，命令详情："+commands + "；标准输出流:"+readOut);
            }
        } catch (IOException | InterruptedException e) {
            result = "进程运行失败，命令详情："+commands + "；错误信息:"+e.getMessage();
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 执行Py文件
     * @param commands Py的Process命令
     * @return
     */
    public static String exePyCommand(String[] commands) {
        String result = "";

        ProcessBuilder processBuilder = new ProcessBuilder();

        // 设置进程命令行参数
        processBuilder.command(commands);
        processBuilder.redirectErrorStream(true);

        StringBuilder readOut = new StringBuilder();
        String lastLine = "";
        try {
            log.info("进程命令开始运行,运行参数：" + Arrays.toString(commands));

            Process process = processBuilder.start();

            // 读取标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                lastLine = line;
                readOut.append(line).append("  ");
            }

            // 等待程序执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            log.info("进程命令运行完毕，" + "退出值为: " + exitValue);

            if (exitValue == 0) {
                result = "成功";
            } else {
//                result = commands[1]+"运行失败";
                result = commands[6]+"运行失败；标准输出流:"+lastLine;
                log.info("进程运行失败，命令详情："+commands + "；标准输出流:"+readOut);
            }
        } catch (IOException | InterruptedException e) {
//            result = commands[1]+"运行失败";
            log.info("进程运行失败，命令详情："+commands + "；错误信息:"+e.getMessage());
            result = commands[6]+"运行失败";
        }
        return result;
    }

    /**
     * 执行docker命令
     * @param commands docker命令
     * @return
     */
    public static String execDockerCommand(String[] commands) {
        String result = "";
        // 设置Docker命令
        ProcessBuilder processBuilder = new ProcessBuilder();

        // 设置命令行参数
        processBuilder.command(commands);

        try {
            log.info("Docker命令开始运行,运行参数：" + Arrays.toString(commands));

            Process process = processBuilder.start();

            StringBuilder readOut = new StringBuilder();
            String lastLine;
            // 读取标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                readOut.append(line).append("  ");
            }

            // 等待程序执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            log.info("docker命令运行完毕，" + "退出值为: " + exitValue);

            if (exitValue == 0) {
                result = "成功";
            } else {
                result = "docker程序运行失败，命令详情："+ Arrays.toString(commands) + "标准输出流:"+readOut;
            }
        } catch (IOException | InterruptedException e) {
            result = "docker命令运行失败，命令详情："+ Arrays.toString(commands) + "；错误信息:"+e.getMessage();
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 时域或频域执行docker命令
     * @param commands docker命令
     * @return
     */
    public static String execBSAndBPDockerCommand(String[] commands) {
        String result = "";
        // 设置Docker命令
        ProcessBuilder processBuilder = new ProcessBuilder();

        // 设置命令行参数
        processBuilder.command(commands);

        try {
            log.info("Docker命令开始运行,运行参数：" + Arrays.toString(commands));

            Process process = processBuilder.start();

            StringBuilder readOut = new StringBuilder();
            String lastLine;
            // 读取标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                readOut.append(line).append("  ");
            }

            // 等待程序执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            log.info("docker命令运行完毕，" + "退出值为: " + exitValue);

            if (exitValue == 1) {
                result = "成功";
            } else {
                result = "时域或频域可执行文件docker程序运行失败，命令详情："+ Arrays.toString(commands) + "标准输出流:"+readOut;
            }
        } catch (IOException | InterruptedException e) {
            result = "时域或频域可执行文件docker命令运行失败，命令详情："+ Arrays.toString(commands) + "；错误信息:"+e.getMessage();
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 执行MkMesh可执行程序的docker命令
     * @param commands docker命令
     * @return
     */
    public static String execDockerMkMeshCommand(String[] commands) {
        String result = "";
        // 设置Docker命令
        ProcessBuilder processBuilder = new ProcessBuilder();

        // 设置命令行参数
        processBuilder.command(commands);

        try {
            log.info("Docker命令开始运行,运行参数：" + Arrays.toString(commands));

            Process process = processBuilder.start();

            StringBuilder readOut = new StringBuilder();
            String lastLine;
            // 读取标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                readOut.append(line).append("  ");
            }

            // 等待程序执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            log.info("docker命令运行完毕，" + "退出值为: " + exitValue);

//            if (exitValue == 0) {
                result = "成功";
//            } else {
//                result = "docker程序运行失败，命令详情："+ Arrays.toString(commands) + "标准输出流:"+readOut;
//            }
        } catch (IOException | InterruptedException e) {
            result = "docker命令运行失败，命令详情："+ Arrays.toString(commands) + "；错误信息:"+e.getMessage();
            e.printStackTrace();
        }
        return result;
    }

    /**
     * parameter参数转换成json文件（MZ固定的，真正联调时需要修改）
     * @param task    task实体
     * @return
     */
    public static String paramToJsonFile(Task task, String containerId, String containerName) {
        String taskType = task.getType();
        String parameter = task.getParameter();
        String res = "";
        boolean flag = true;
        JSONObject json = JSON.parseObject(parameter);

//        log.info("若inp_path字段是否存在，存在则校验是否存在inp文件");
//        if(ObjectUtils.isNotEmpty(json.getString("inp_path"))) {
//            String inp_path = json.getString("inp_path");
//            if (FileUtil.isFileExist(inp_path) == false || !FileUtil.checkFileType(inp_path, "inp") ) {
//                log.info(inp_path+"文件不存在或文件后缀名不为inp");
//                return inp_path+"文件不存在或文件后缀名不为inp";
//            }
//        }

        if (Objects.equals(taskType, "BP") || Objects.equals(taskType, "bp")) {
            log.info("生成BP对应可执行程序用到的新的.json文件");
            //板块元频域任务BP
            //板块元频域任务用到的参数
            try {
                JSONArray hor = json.getJSONArray("hor");
                JSONArray ver = json.getJSONArray("ver");
                JSONArray freq = json.getJSONArray("freq");
                float radius = json.getFloat("radius");
                JSONArray center = json.getJSONArray("model_coo_center");
                JSONArray right = json.getJSONArray("model_coo_right");
                JSONArray head = json.getJSONArray("model_coo_head");
//                String jsonFile = "/root/tsbsDuiJie/ts/freq.json";
                String jsonFile = "/root/tsbsDuiJie/ts/"+task.getId()+"/freq.json";


                //真正联调时ddt_path、BoundaryFileName换成part.json对应内容
////                String[] files = {"./ts_file/water_steel(8mm)_water_V.dat", "./ts_file/water_steel(4mm)_air_V.dat",
////                        "./ts_file/water_steel(8mm)_water_T.dat", "./ts_file/water_AL(10mm)_air_T.dat"};

//                JSONObject part = JSON.parseObject(json.getString("part").substring(1,json.getString("part").length()-1));
//                JSONArray material = part.getJSONArray("material");
                log.info("获取板块元计算用的freq.json用到的各r_dat_path、t_dat_path");
                String materialJson = "";
                JSONArray part = json.getJSONArray("part");
                String[] files = new String[part.size()*2];
                int index = 0;
                for(int i = 0; i < part.size(); i++) {
                    if( ObjectUtils.isNotEmpty(part.getJSONObject(i).getString("material")) ) {
                        materialJson = part.getJSONObject(i).getString("material");
                    }
                    JSONObject material = JSON.parseObject(materialJson);
                    String r_dat_path = material.getString("r_dat_path");
                    String t_dat_path = material.getString("t_dat_path");
                    files[index] = r_dat_path;
                    files[index + 1] = t_dat_path;
                    index = index + 2;
                }

                System.out.println("r_dat_path、t_dat_path获取完成");
                System.out.println("详情："+ Arrays.toString(files));
//                for (int i = 0; i < files.length; i++) {
//                    if(!FileUtil.isFileExist(files[i]) || !FileUtil.checkFileType(files[i], "dat")) {
//                        return files[i] + "文件不存在或文件后缀名不为dat";
//                    }
//                }

                //生成可执行程序inp_to_ddt用的part.json
                log.info("part参数转换成json文件，生成BP可执行程序inp_to_ddt用的part.json");
//                String jsonFilePath = "/root/tsbsDuiJie/inp2ddt/"+task.getId()+"/part.json";
                String jsonFilePath = "/root/tsbsDuiJie/inp2ddt/part.json";
                flag = FileUtil.creatJsonFile(JSON.toJSONString(json.getJSONArray("part")), jsonFilePath);
                if(!flag) {
                    return jsonFilePath+"文件生成失败";
                }
                //调用可执行程序inp_to_ddt，用inp、part.json生成DDT文件
                log.info("调用可执行程序inp_to_ddt，用inp、part.json生成DDT文件");
                String DDT_PATH = "/root/tsbsDuiJie/ts/ts_file/"+task.getId()+"/bechmark_double_all.DDT";
                FileUtil.isFileFlowExistAndCreate(FileUtil.getMkdirPath(DDT_PATH));
                if(ObjectUtils.isEmpty(json.getString("inp_path")) ) {
                    return "inp_path参数不存在为空";
                } else if(!FileUtil.isFileExist(json.getString("inp_path"))) {
                    return json.getString("inp_path")+"文件不存在";
                }
                String inp_path = json.getString("inp_path");
                if (!FileUtil.isFileExist(inp_path) || !FileUtil.checkFileType(inp_path, "inp") ) {
                    log.info(inp_path+"文件不存在或文件后缀名不为inp");
                    return inp_path+"文件不存在或文件后缀名不为inp";
                }
                String directory = "/root/tsbsDuiJie";
//                String[] command = {"docker", "exec", "-w", directory, containerName, "./inp_to_ddt", inp_path, jsonFilePath, DDT_PATH};
//                String[] command = {"podman", "exec", "-w", directory, containerName, "./inp_to_ddt", inp_path, jsonFilePath, DDT_PATH};
                String pyPath = "/root/tsbsDuiJie/inp2ddt/inp2ddt.py";
                String[] command = {"podman", "exec", "-w", directory, containerName, "python3", pyPath, inp_path, jsonFilePath, DDT_PATH};
                res = UseUtil.execDockerCommand(command);
                if(res != "成功") {
                    return "可执行程序inp_to_ddt运行失败,未能生成DDT文件";
                }
                if(!FileUtil.isFileExist(DDT_PATH) || !FileUtil.checkFileType(DDT_PATH, "DDT")) {
                    return DDT_PATH + "文件不存在或文件后缀名不为DDT";
                }

                log.info("BP板块元频域开始生成赋值");
                BP bp = new BP();
                bp.setDdt_path(DDT_PATH);
                bp.setBoundaryFileName(files);
                System.out.println("ddt_path、BoundaryFileName赋值成功");
                bp.setHor_angle_start(hor.getFloat(0));
                bp.setHor_angle_end(hor.getFloat(1));
                bp.setHor_angle_step(hor.getFloat(2));
                bp.setVer_angle_start(ver.getFloat(0));
                bp.setVer_angle_end(ver.getFloat(1));
                bp.setVer_angle_step(ver.getFloat(2));
                bp.setFreq_low(freq.getFloat(0));
                bp.setFreq_high(freq.getFloat(1));
                bp.setFreq_step(freq.getFloat(2));
                bp.setM_center_x(center.getFloat(0));
                bp.setM_center_y(center.getFloat(1));
                bp.setM_center_z(center.getFloat(2));
                bp.setM_right_x(right.getFloat(0));
                bp.setM_right_y(right.getFloat(1));
                bp.setM_right_z(right.getFloat(2));
                bp.setM_head_x(head.getFloat(0));
                bp.setM_head_y(head.getFloat(1));
                bp.setM_head_z(head.getFloat(2));
                bp.setRadius(radius);
                System.out.println("hor、ver、freq、radius、model_coo_center、model_coo_right、model_coo_head赋值成功");
                log.info("开始生成freq.json文件");
                flag = FileUtil.creatJsonFile(JSON.toJSONString(bp), jsonFile);
                if(flag == false) {
                    return jsonFile+"文件生成失败";
                } else {
                    log.info("freq.json文件生成"+flag+"，taskId = "+task.getId());
                    return "成功";
                }
            } catch (Exception e) {
                log.error("BP板块元频域任务parameter参数转换成json文件失败，parameter参数为"+json+
                        "；请检查hor、ver、freq、radius、model_coo_center、model_coo_right、model_coo_head、inp_path参数情况"+e.getMessage());
                return "BP板块元频域任务parameter参数异常；请检查hor、ver、freq、radius、model_coo_center、model_coo_right、model_coo_head、inp_path参数情况";
            }

        }
        else if (Objects.equals(taskType, "BS") || Objects.equals(taskType, "bs")) {
            log.info("生成BS对应可执行程序用到的新的time_light.json文件");
            //板块元时域任务BS
            //板块元时域任务BS用到的参数
            try {
                JSONArray hor = json.getJSONArray("hor");
                JSONArray freq = json.getJSONArray("freq");
                float radius = json.getFloat("radius");
                float ver = json.getFloat("ver");
                float cent_freq = json.getFloat("cent_freq");
                float pulse_width = json.getFloat("pulse_width");
                JSONArray center = json.getJSONArray("model_coo_center");
                JSONArray right = json.getJSONArray("model_coo_right");
                JSONArray head = json.getJSONArray("model_coo_head");
//                String jsonFile = "/root/tsbsDuiJie/time_light/time_light.json";
                String jsonFile = "/root/tsbsDuiJie/time_light/"+task.getId()+"/time_light.json";


//                //真正联调时ddt_path、BoundaryFileName换成part.json对应内容
//
////                String[] files = {"./test_file/water_steel(8mm)_water_V.dat", "./test_file/water_steel(4mm)_air_V.dat",
////                        "./test_file/water_steel(8mm)_water_T.dat", "./test_file/water_AL(10mm)_air_T.dat"};
//                JSONObject part = JSON.parseObject(json.getString("part").substring(1,json.getString("part").length()-1));
//                JSONArray material = part.getJSONArray("material");

                log.info("获取板块元计算用的time_light.json用到的各r_dat_path、t_dat_path");
                String materialJson = "";
//                JSONArray part = json.getJSONArray("part");
//                for(int i = 0; i < part.size(); i++) {
//                    if( ObjectUtils.isNotEmpty(part.getJSONObject(i).getString("material")) ) {
//                        materialJson = part.getJSONObject(i).getString("material");
//                    }
//                }
//                JSONObject material = JSON.parseObject(materialJson);
//                String r_dat_path = material.getString("r_dat_path");
//                String t_dat_path = material.getString("t_dat_path");
//                String[] files = {r_dat_path, t_dat_path};
                System.out.println("part:"+json.getJSONArray("part"));
                JSONArray part = json.getJSONArray("part");
                String[] files = new String[part.size()*2];
                int index = 0;
                for(int i = 0; i < part.size(); i++) {
                    if( ObjectUtils.isNotEmpty(part.getJSONObject(i).getString("material")) ) {
                        materialJson = part.getJSONObject(i).getString("material");
                    }
                    JSONObject material = JSON.parseObject(materialJson);
                    String r_dat_path = material.getString("r_dat_path");
                    String t_dat_path = material.getString("t_dat_path");
                    files[index] = r_dat_path;
                    files[index + 1] = t_dat_path;
                    index = index + 2;
                }

                System.out.println("r_dat_path、t_dat_path获取完成");
//                for (int i = 0; i < files.length; i++) {
//                    if(FileUtil.isFileExist(files[i]) == false || !FileUtil.checkFileType(files[i], "dat") ) {
//                        return files[i] + "文件不存在或文件后缀名不为dat";
//                    }
//                }

                //part参数转换成json文件
                log.info("part参数转换成json文件，生成BS可执行程序inp_to_ddt用的part.json");
//                String jsonFilePath = "/root/tsbsDuiJie/inp2ddt/"+task.getId()+"/part.json";
                String jsonFilePath = "/root/tsbsDuiJie/inp2ddt/part.json";
                flag = FileUtil.creatJsonFile(JSON.toJSONString(json.getJSONArray("part")), jsonFilePath);
                if(flag == false) {
                    return jsonFilePath+"文件生成失败";
                }
                //调用可执行程序inp_to_ddt，用inp、part.json生成DDT文件
                log.info("调用inp_to_ddt可执行程序，用inp、part.json生成DDT文件");
                String DDT_PATH = "/root/tsbsDuiJie/time_light/test_file/"+task.getId()+"/bechmark_double_all.DDT";
                FileUtil.isFileFlowExistAndCreate(FileUtil.getMkdirPath(DDT_PATH));
                if(ObjectUtils.isEmpty(json.getString("inp_path")) ) {
                    return "inp_path参数不存在为空";
                } else if(!FileUtil.isFileExist(json.getString("inp_path"))) {
                    return json.getString("inp_path")+"文件不存在";
                }
                String inp_path = json.getString("inp_path");
                if (!FileUtil.isFileExist(inp_path) || !FileUtil.checkFileType(inp_path, "inp") ) {
                    log.info(inp_path+"文件不存在或文件后缀名不为inp");
                    return inp_path+"文件不存在或文件后缀名不为inp";
                }
                String directory = "/root/tsbsDuiJie/inp2ddt";
//                String[] command = {"docker", "exec", "-w", directory, containerName, "./inp_to_ddt", inp_path, jsonFilePath, DDT_PATH};
//                String[] command = {"podman", "exec", "-w", directory, containerName, "./inp_to_ddt", inp_path, jsonFilePath, DDT_PATH};
                String pyPath = "/root/tsbsDuiJie/inp2ddt/inp2ddt.py";
                String[] command = {"podman", "exec", "-w", directory, containerName, "python3", pyPath, inp_path, jsonFilePath, DDT_PATH};

                res = UseUtil.execDockerCommand(command);
                if(res != "成功") {
                    return "可执行程序inp_to_ddt运行失败,未能生成DDT文件";
                }
                if(!FileUtil.isFileExist(DDT_PATH) || !FileUtil.checkFileType(DDT_PATH, "DDT")) {
                    return DDT_PATH + "文件不存在或文件后缀名不为DDT";
                }
                if(FileUtil.isFileExist(DDT_PATH) == false || !FileUtil.checkFileType(DDT_PATH, "DDT") ) {
                    return DDT_PATH + "文件不存在或文件后缀名不为DDT";
                }

                log.info("BS板块元时域开始生成赋值");
                BS data = new BS();
                data.setDdt_path(DDT_PATH);
                data.setBoundaryFileName(files);
                System.out.println("ddt_path、BoundaryFileName赋值成功");
                data.setHor_angle_start(hor.getFloat(0));
                data.setHor_angle_end(hor.getFloat(1));
                data.setHor_angle_step(hor.getFloat(2));
                data.setFreq_low(freq.getFloat(0));
                data.setFreq_high(freq.getFloat(1));
                data.setFreq_step(freq.getFloat(2));
                data.setM_center_x(center.getFloat(0));
                data.setM_center_y(center.getFloat(1));
                data.setM_center_z(center.getFloat(2));
                data.setM_right_x(right.getFloat(0));
                data.setM_right_y(right.getFloat(1));
                data.setM_right_z(right.getFloat(2));
                data.setM_head_x(head.getFloat(0));
                data.setM_head_y(head.getFloat(1));
                data.setM_head_z(head.getFloat(2));
                data.setRadius(radius);
                data.setVer_angle(ver);
                data.setCent_freq(cent_freq);
                data.setPulse_width(pulse_width);
                System.out.println("hor、freq、radius、ver、cent_freq、pulse_width、model_coo_center、model_coo_right、model_coo_head赋值成功");
                log.info("生成time_light.json文件");
                flag = FileUtil.creatJsonFile(JSON.toJSONString(data), jsonFile);
                if(flag == false) {
                    return jsonFile+"文件生成失败";
                } else {
                    log.info("time_light.json文件生成" + flag);
                    return "成功";
                }
            } catch (Exception e) {
                log.error("BS板块元时域任务parameter参数转换成json文件失败，parameter参数为"+json+
                        "；请检查hor、freq、radius、ver、cent_freq、pulse_width、model_coo_center、model_coo_right、model_coo_head、inp_path参数情况"+e.getMessage());
                return "BS板块元时域任务parameter参数异常；请检查hor、freq、radius、ver、cent_freq、pulse_width、model_coo_center、model_coo_right、model_coo_head、inp_path参数情况";
            }
        }
        else if (Objects.equals(taskType, "MZ") || Objects.equals(taskType, "mz")) {
            log.info("MZ-par对应生成参数化建模genGeoModel的data.json文件暂固定，不覆盖");
            try {
                String mzType = json.getString("type");
                String model_path = json.getString("model_path"); //存放输出文件
                FileUtil.isFileFlowExistAndCreate(model_path);

                log.info("MZ生成模型计算part.json文件");
                //仅针对模型整备任务，type=='MESH'情况，生成part.json文件  CAD、PAR情况的json文件暂固定
                if (Objects.equals(mzType, "MESH") || Objects.equals(mzType, "mesh")) {
                    log.info("生成MZ模型整备任务，type=='MESH'情况，mphtxt生成part.json文件");
                    JSONArray input_fileLists = json.getJSONArray("input_files");
//                String[] input_files = (String[]) input_fileLists.toArray();
                    String[] input_files = new String[input_fileLists.size()];
                    for (int i = 0; i < input_fileLists.size(); i++) {
                        if (FileUtil.isFileExist(input_fileLists.getString(i)) == false || !FileUtil.checkFileType(input_fileLists.getString(i), "mphtxt")) {
                            return input_fileLists.getString(i) + "文件不存在或文件后缀名不为mphtxt";
                        }
//                    //复制文件并且将中文转换成拼音
//                    FileUtil.copyFileAndToPInYIn(input_fileLists.getString(i), "/root/tsbsDuiJie/model/mesh");
//                    input_files[i] = FileUtil.toPinyin(input_fileLists.getString(i));

                    }

                    //调用process_mesh_inp.py脚本，直接在model_path下生成part.json文件
                    String pyFilePath = "/root/tsbsDuiJie/model/mesh/process_mesh_inp.py";
                    String json_path = model_path + "/part.json";
//                    //  python3 /root/tsbsDuiJie/model/mesh/process_mesh_inp.py [/root/BS/model/2024-03/5/1.mphtxt,/root/BS/model/2024-03/5/2.mphtxt] ./part.json
//                    res = UseUtil.callMZMeshPython(pyFilePath, input_files, json_path);
                    String inputPath = "";
                    for (String input_file : input_files) {
                        inputPath += ",'" + input_file + "'";
                    }
                    String inputFiles = "[" + inputPath.substring(1) + "]";
                    String directory = "/root/tsbsDuiJie";
//                    String[] command = {"docker", "exec", "-w", directory, containerName, "python3", pyFilePath, inputFiles, json_path};
                    String[] command = {"podman", "exec", "-w", directory, containerName, "python3", pyFilePath, inputFiles, json_path};
                    res = UseUtil.execDockerCommand(command);
                    if (!Objects.equals(res, "成功")) {
                        return "part.json文件生成失败";
                    }
                    //为方便查看，将生成文件复制到测试路径下
                    FileUtil.copyFile(json_path, "/root/tsbsDuiJie/model/mesh");
                }
                else if (Objects.equals(mzType, "PAR") || Objects.equals(mzType, "par")) {
                    //参数化建模的参数输入params转换成data.json文件，并替换到docker容器下
                    log.info("生成MZ参数化建模genGeoModel的参数输入params转换成data.json文件");
                    JSONObject params = JSON.parseObject(json.getString("params"));
                    if(ObjectUtils.isEmpty(params)) {
                        return "参数异常！请检查params参数情况！";
                    }
                    flag = FileUtil.creatJsonFile(JSON.toJSONString(params), "/root/tsbsDuiJie/genGeoModel/data.json");
                    if(flag == false) {
                        return "参数化建模genGeoModel的data.json文件生成失败";
                    }
                    log.info("模型整备任务，type=='PAR'情况参数化建模genGeoModel的data.json文件件生成" + flag);

//                    //生成网格划分mkmesh的data.json文件，并替换到docker容器下
//                    log.info("生成模型整备任务，type=='PAR'情况网格划分mkmesh的json文件");
//                    float maxsize = json.getFloat("maxsize");
//                    String type = "doSurfaceMesh";
//                    String inp_path = model_path + "/input_par.inp";
//                    JSONArray input_files = json.getJSONArray("input_files");
////                    String[] input_files_array = new String[input_files.size()];
////                    for (int i = 0; i < input_files.size(); i++) {
////                        input_files_array[i] = input_files.getString(i);
////                        if (FileUtil.isFileExist(input_files.getString(i)) == false
////                                || !(FileUtil.checkFileType(input_files.getString(i),"stp") || FileUtil.checkFileType(input_files.getString(i),"step")) ) {
////                            return input_files.getString(i) + "文件不存在或文件后缀名不为stp或step";
////                        }
////                    }
//                    String[] input_files_array = {params.getString("stp_file")};
//                    if(ObjectUtils.isEmpty(params.getString("stp_file")) || !(FileUtil.checkFileType(input_files_array[1],"stp") || FileUtil.checkFileType(input_files_array[1],"step")) ) {
//                        return "stp_file参数异常！请检查stp_file参数情况！";
//                    }
//                    MZ data = new MZ();
//                    data.setType(type);
//                    data.setInput_files(input_files_array);
//                    data.setModel_path(inp_path);
//                    data.setParams("[]");
//                    data.setMaxsize(maxsize);
//                    flag = FileUtil.creatJsonFile(JSON.toJSONString(data), "/root/tsbsDuiJie/mkmesh/data.json");
//                    if(flag == false) {
//                        return "网格划分mkmesh的data.json文件生成失败";
//                    }
//                    log.info("模型整备任务，type=='PAR'情况网格划分mkmesh的data.json文件生成" + flag);
                }
                else if (Objects.equals(mzType, "CAD") || Objects.equals(mzType, "cad")) {
                    log.info("生成MZ模型整备任务，type=='CAD'情况网格划分mkmesh的json文件");

                    String type = "doSurfaceMesh";
                    String inp_path = model_path + "/input_cad.inp";
                    JSONArray input_files = json.getJSONArray("input_files");
                    String[] input_files_array = new String[input_files.size()];
                    for (int i = 0; i < input_files.size(); i++) {
                        input_files_array[i] = input_files.getString(i);
                        if (FileUtil.isFileExist(input_files.getString(i)) == false
                                || !(FileUtil.checkFileType(input_files.getString(i),"stp")
                                || FileUtil.checkFileType(input_files.getString(i),"step")
                                || FileUtil.checkFileType(input_files.getString(i),"igs") ) ) {
                            return input_files.getString(i) + "文件不存在或文件后缀名不为stp或step或igs";
                        }
                    }
                    if(ObjectUtils.isEmpty(json.getFloat("maxsize")) || ObjectUtils.isEmpty(input_files) || ObjectUtils.isEmpty(model_path) ) {
                        return "参数异常！请检查maxsize参数情况！"+json;
                    }
                    float maxsize = json.getFloat("maxsize");
                    MZ data = new MZ();
                    data.setType(type);
                    data.setInput_files(input_files_array);
                    data.setModel_path(inp_path);
                    data.setMaxsize(maxsize);
                    flag = FileUtil.creatJsonFile(JSON.toJSONString(data), "/root/tsbsDuiJie/mkmesh/data.json");
                    log.info("data.json文件生成" + flag);
                }
                else {
                    log.error("MZ模型整备任务type为"+mzType+"，请检查type、params、input_files、model_path、maxsize参数情况");
                    return "MZ模型整备任务type为"+mzType+"，请检查type、params、input_files、model_path、maxsize参数情况";
                }
            } catch (Exception e) {
                log.error("MZ模型整备任务parameter参数转换成json文件失败，parameter参数为"+json+
                        "；请检查type、params、input_files、model_path、maxsize参数情况"+e.getMessage());
                return "MZ模型整备任务parameter参数异常；请检查type、params、input_files、model_path、maxsize参数情况";
            }
        }
        else if (Objects.equals(taskType, "CZ") || Objects.equals(taskType, "cz")) {
            log.info("CZ生成可执行程序用到的data.json文件");
            try {
                String mzType = json.getString("type");
                String result_path = json.getString("result_path"); //存放输出文件
                FileUtil.isFileFlowExistAndCreate(result_path);

                //仅针对材料整备任务，type=='CAD'情况，生成json文件
                if (Objects.equals(mzType, "CAD") || Objects.equals(mzType, "cad")) {
                    log.info("CZ-cad对应网格划分mkmesh可执行程序用到的data.json文件");
                    String inp_path = result_path + "/input_cad.inp";

                    String type = "doSurfaceMesh";
//                    JSONArray input_files = json.getJSONArray("cad_path");
//                    String[] input_files_array = new String[input_files.size()];
//                    for (int i = 0; i < input_files.size(); i++) {
//                        input_files_array[i] = input_files.getString(i);
//                        if(FileUtil.isFileExist(input_files.getString(i)) == false
//                                || !(FileUtil.checkFileType(input_files.getString(i),"stp") || FileUtil.checkFileType(input_files.getString(i),"step")) ) {
//                            return input_files.getString(i) + "文件不存在或文件后缀名不为stp或step";
//                        }
//                    }
                    String input_files = json.getString("cad_path");
                    String[] input_files_array = {input_files};
                    if(FileUtil.isFileExist(input_files) == false
                            || !(FileUtil.checkFileType(input_files,"stp") || FileUtil.checkFileType(input_files,"step")) ) {
                        return input_files + "文件不存在或文件后缀名不为stp或step";
                    }

                    if(ObjectUtils.isEmpty(json.getFloat("maxsize")) || ObjectUtils.isEmpty(input_files)  ) {
                        return "参数异常！请检查cad_path、maxsize参数情况！"+json;
                    }
                    float maxsize = json.getFloat("maxsize");
                    MZ data = new MZ();
                    data.setType(type);
                    data.setInput_files(input_files_array);
                    data.setModel_path(inp_path);
                    data.setMaxsize(maxsize);
                    flag = FileUtil.creatJsonFile(JSON.toJSONString(data), "/root/tsbsDuiJie/mkmesh/data.json");
                    if(flag == false) {
                        return "mkmesh可执行程序的data.json文件生成失败"+data.toString();
                    }
                    log.info("data.json文件生成" + flag);
                }
                else if (Objects.equals(mzType, "mesh") || Objects.equals(mzType, "MESH")) {
                    res = "成功";
                }
                else if (Objects.equals(mzType, "MATERIAL") || Objects.equals(mzType, "material")) {
                    res = "成功";
                }
                else {
                    log.error("CZ模型整备任务type为"+mzType+"，请检查type参数情况");
                    return "CZ模型整备任务type为"+mzType+"，请检查type参数情况";
                }
            } catch (Exception e) {
                log.error("材料整备任务CZ参数异常，parameter参数为" + json + "；请检查cad_path、type、material_path、maxsize参数情况;"+e.getMessage());
                return "CZ材料整备任务parameter参数异常；请检查cad_path、type、material_path、maxsize参数情况";
            }
        }
        else if (Objects.equals(taskType, "JP") || Objects.equals(taskType, "jp")) {
            try {
                String params = json.getString("params");
                flag = FileUtil.creatJsonFile(params, "/root/tsbsDuiJie/analyze/data.json");
                if(flag == false) {
                    log.info("解析解params建模参数转data.json文件失败，请检查params参数, params:" + params);
                    return "解析解params建模参数转data.json文件失败，请检查params参数";
                }
                log.info("data.json文件生成" + flag +"路径：/root/tsbsDuiJie/analyze/data.json");
            } catch (Exception e) {
                log.error("解析解params建模参数转data.json文件失败，请检查params参数;"+e.getMessage());
                return "解析解params建模参数转data.json文件失败，请检查params参数;";
            }
        }

        if(flag == false) {
            res = "json文件存储失败";
        } else {
            res = "成功";
        }
        return res;
    }


    /**
     * MZ模型整备PAR参数化建模parameter参数转换成json文件
     * @param parameter
     * @param inp_path  json文件中要生成的inp文件路径
     * @return
     */
    public static String createMkMeshJsonFile(String parameter,String stp_path,String inp_path) {
        String res = "";
        Boolean flag = false;
        JSONObject json = JSON.parseObject(parameter);

        log.info("MZ-par情况网格划分mkmesh用到的data.json文件开始生成，stp文件路径："+stp_path);
        try {
            String mzType = json.getString("type");
            String model_path = json.getString("model_path"); //存放输出文件
            FileUtil.isFileFlowExistAndCreate(model_path);

            if (Objects.equals(mzType, "PAR") || Objects.equals(mzType, "par")) {
                //参数化建模的参数输入params转换成data.json文件，并替换到docker容器下
                log.info("生成模型整备任务，type=='PAR'情况网格划分mkmesh的json文件");

                String type = "doSurfaceMesh";
//                JSONArray input_files = json.getJSONArray("input_files");
//                String[] input_files_array = new String[input_files.size()];
//                for (int i = 0; i < input_files.size(); i++) {
//                    input_files_array[i] = input_files.getString(i);
//                    if (FileUtil.isFileExist(input_files.getString(i)) == false
//                            || !(FileUtil.checkFileType(input_files.getString(i),"stp") || FileUtil.checkFileType(input_files.getString(i),"step")) ) {
//                        return input_files.getString(i) + "文件不存在或文件后缀名不为stp或step";
//                    }
//                }
                if(FileUtil.isFileExist(stp_path) == false
                        || !(FileUtil.checkFileType(stp_path,"stp") || FileUtil.checkFileType(stp_path,"step")) ){
                    return stp_path + "文件不存在或文件后缀名不为stp或step";
                }
                String[] input_files_array = {stp_path};
                if(ObjectUtils.isEmpty(json.getFloat("maxsize"))) {
                    return "maxsize参数为空";
                }

                float maxsize = json.getFloat("maxsize");
                MZ data = new MZ();
                data.setType(type);
                data.setInput_files(input_files_array);
                data.setModel_path(inp_path);
                data.setParams("[]");
                data.setMaxsize(maxsize);
                flag = FileUtil.creatJsonFile(JSON.toJSONString(data), "/root/tsbsDuiJie/mkmesh/data.json");
                if(flag == true) {
                    log.info("data.json文件生成" + flag);
                    res = "成功";
                } else {
                    res = "MZ模型整备PAR参数化建模parameter参数转换成json文件失败";
                }
            }
        } catch (Exception e) {
            log.error("MZ模型整备任务parameter参数转换成json文件失败，parameter参数为" + json + "；请检查type、params、input_files、model_path参数情况;"+e.getMessage());
            return "MZ模型整备任务parameter参数异常";
        }

        return res;
    }

    /**
     * 依据任务类型，合并dat文件
     * @param taskType
     * @param targetPath
     * @return
     */
    public static String concatAndCopyDatFile(String taskType,String targetPath) {
        log.info("依据任务类型，合并dat文件");
        String result = "";

        if (Objects.equals(taskType, "BP") || Objects.equals(taskType, "bp")) {
            //板块元频域任务BP
//            String[] filePath = {"/root/tsbsDuiJie/freq_0.dat","/root/tsbsDuiJie/freq_1.dat",
//                    "/root/tsbsDuiJie/freq_2.dat","/root/tsbsDuiJie/freq_3.dat"};
            String[] filePath = {"/root/tsbsDuiJie/ts/freq_0.dat","/root/tsbsDuiJie/ts/freq_1.dat",
                    "/root/tsbsDuiJie/ts/freq_2.dat","/root/tsbsDuiJie/ts/freq_3.dat"};
            String tarFilePath = targetPath+"/freq_result.dat";
            String flag = FileUtil.concatDatFiles(filePath,tarFilePath);
            if(Objects.equals(flag, "成功")) {
                FileUtil.deleteFiles(filePath);
            } else {
                return "BP板块元频域任务"+flag;
            }
        } else if (Objects.equals(taskType, "BS") || Objects.equals(taskType, "bs")) {
            //板块元时域任务BS
            //合并时域回波结果文件  ---  脉冲回波
//            String[] filePath1 = {"/root/tsbsDuiJie/time_0.dat","/root/tsbsDuiJie/time_1.dat",
//                    "/root/tsbsDuiJie/time_2.dat","/root/tsbsDuiJie/time_3.dat"};
            String[] filePath1 = {"/root/tsbsDuiJie/time_light/time_0.dat","/root/tsbsDuiJie/time_light/time_1.dat",
                    "/root/tsbsDuiJie/time_light/time_2.dat","/root/tsbsDuiJie/time_light/time_3.dat"};
            String tarFilePath1 = targetPath+"/time.dat";
            String flag2 = FileUtil.concatDatFiles(filePath1,tarFilePath1);

            if(Objects.equals(flag2, "成功")) {
                FileUtil.deleteFiles(filePath1);
            } else {
                return "BS板块元时域任务"+flag2;
            }
            //合并回波亮点结果文件  ---  回波亮点
//            String[] filePath2 = {"/root/tsbsDuiJie/light_0.dat","/root/tsbsDuiJie/light_1.dat",
//                    "/root/tsbsDuiJie/light_2.dat","/root/tsbsDuiJie/light_3.dat"};
            String[] filePath2 = {"/root/tsbsDuiJie/time_light/light_0.dat","/root/tsbsDuiJie/time_light/light_1.dat",
                    "/root/tsbsDuiJie/time_light/light_2.dat","/root/tsbsDuiJie/time_light/light_3.dat"};
            String tarFilePath2 = targetPath+"/light_result.dat";
            String flag3 = FileUtil.concatDatFiles(filePath2,tarFilePath2);
            if(Objects.equals(flag3, "成功")) {
                FileUtil.deleteFiles(filePath2);
            } else {
                return "BS板块元时域任务"+flag3;
            }
        }

        result = "成功";
        return result;
    }

    /**
     * 调用频域、时域可执行文件
     * @param exeFilePath  频域可执行文件freq_cal的路径
     * @param jsonFilePath 可执行文件调用的json文件路径
     * @param poolSize     线程池的大小
     * @param poolIndex    线程下标
     * @return
     */
    public static String callExe(String exeFilePath, String jsonFilePath, String directory,Integer poolSize, Integer poolIndex) {
        log.info("调用可执行文件"+exeFilePath);
        String result = "";
//        String exePath = "/root/tsbsDuiJie/freq_cal";
//        String jsonPath = "/root/tsbsDuiJie/freq.json";
//        String type = "0";
//        String runNumber = "3";
//        String index = "0";
        String exePath = exeFilePath;
        String jsonPath = jsonFilePath;
        String runNumber = String.valueOf(poolSize);
        String index = String.valueOf(poolIndex);
        if(!FileUtil.isFileExist(exePath)) {
            log.info(exePath+"可执行文件文件不存在");
            return exePath+"可执行文件文件不存在";
        }
        if(!FileUtil.isFileExist(jsonPath)) {
            log.info(jsonPath+"json文件不存在");
            return jsonPath+"json文件不存在";
        }
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(exePath, jsonPath, runNumber,index);
        processBuilder.directory(new java.io.File(directory));
        processBuilder.redirectErrorStream(true);
        try {
            String[] commands = new String[]{exePath, jsonPath, runNumber,index};
            log.info("可执行程序开始运行,运行参数："+Arrays.toString(commands));

            Process process = processBuilder.start();

            StringBuilder readOut = new StringBuilder();
            String lastLine;
            // 读取标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                readOut.append(line).append("  ");
            }

            // 等待程序执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
            log.info("程序"+exePath+"第"+(poolIndex+1)+"个线程运行完毕，"+"退出值为: " + exitValue);
            if(exitValue == 0) {
                result = "成功";
            } else {
                return "程序"+exePath+"第"+(poolIndex+1)+"个线程运行失败" + "标准输出流:"+readOut;
            }
        } catch (IOException | InterruptedException e) {
            log.info(exeFilePath+"运行失败;"+e.getMessage());
            e.printStackTrace();
            return "程序"+exePath+"第"+(poolIndex+1)+"个线程运行失败";
        }
        return result;
    }

    /**
     * 调用python脚本,仅针对模型整备任务，type=='MESH'情况，生成part.json文件
     * @param pyFilePath  python脚本的路径   /root/tsbsDuiJie/model/mesh/process_mesh_inp.py
     * @param input_files inp文件输入文件路径数组
     * @param json_path   生成的json文件结果路径，即model_path
     */
    public static String callMZMeshPython(String pyFilePath, String[] input_files, String json_path) {
        String result = "";

        if (!FileUtil.isFileExist(pyFilePath)) {
            return pyFilePath + "python脚本不存在";
        }

        String inputPath = "";
        for (String input_file : input_files) {
            inputPath += ",'" + input_file + "'";
        }
        String input_fileLists = "[" + inputPath.substring(1) + "]";


        String[] arguments = new String[]{"python3", pyFilePath, input_fileLists, json_path};

        try {
            log.info("模型类型为Mesh时调用对应py，命令行参数：" + Arrays.toString(arguments));
            ProcessBuilder builder = new ProcessBuilder(arguments);
            Process p = builder.start();
            StringBuilder readOut = new StringBuilder();
            String lastLine = "";
            // 读取Python脚本的输出
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                lastLine = line;
                readOut.append(line).append("  ");
            }
            in.close();

            // 等待Python脚本执行完成
            p.waitFor();
            // 获取退出值
            int exitValue = p.exitValue();

            log.info(pyFilePath + "运行完毕，退出值为：" + exitValue);
            if(exitValue == 0) {
                result = "成功";
            } else {
                log.info(pyFilePath+"运行失败，命令详情："+arguments + "；标准输出流:"+readOut);
                return pyFilePath+"运行失败" + "；标准输出流:"+lastLine;
            }
        } catch (IOException e) {
            log.info(pyFilePath+"脚本运行失败;"+e.getMessage());
            e.printStackTrace();
        } catch (InterruptedException e) {
            log.info(pyFilePath+"脚本运行失败;"+e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 调用频域Python脚本并将结果文件复制到指定文件夹
     * @param pyFilePath "/root/tsbs/ts/gltf_interface.py"
     * @param parameter json字符串
     * @param containerName  容器名称
     */
    public static String callBPPythonAndCopy(String pyFilePath, String parameter, String containerName) {
        String result = "";

        if (!FileUtil.isFileExist(pyFilePath)) {
            return pyFilePath + "不存在";
        }

        JSONObject json = JSON.parseObject(parameter);
        String inp_path = json.getString("inp_path");
        String result_path = json.getString("result_path");
        String name = "guapi.gltf";
        //转换成Python脚本对应参数 创建numpy数组的字符串表示，例如："np.array([1, 2, 3])"
        String hor = json.getString("hor");
        String ver = json.getString("ver");
        String freq = json.getString("freq");
        String radius = json.getString("radius");
        String center = json.getString("model_coo_center");
        String right = json.getString("model_coo_right");
        String head = json.getString("model_coo_head");
        String nameLightPath = "light.gltf";

        if (!FileUtil.isFileExist(inp_path)) {
            return inp_path + "不存在";
        }
        FileUtil.copyFile(inp_path, "/root/tsbsDuiJie/ts");//为方便查看，将inp文件复制到测试路径下

        //python3 /root/tsbsDuiJie/ts/guapi_mesh.py [0, 360, 1] [60, 120, 1] 50.0 [31.0, 0.0, 0.0] [0.0, 1.0, 0.0] [-1.0, 0.0, 0.0] gua.gltf
//        String[] arguments = new String[]{"python3", pyFilePath, hor, ver, radius, center, right, head, name};
        String directory = "/root/tsbsDuiJie";
//        String[] arguments = {"docker", "exec", "-w", directory, containerName, "python3", pyFilePath, hor, ver, radius, center, right, head, name};
        String[] arguments = {"podman", "exec", "-w", directory, containerName, "python3", pyFilePath, hor, ver, radius, center, right, head, name};
        try {
//            Process p = Runtime.getRuntime().exec(arguments);// 执行
            log.info("调用python脚本" + pyFilePath + "，命令行参数：" + Arrays.toString(arguments));
            ProcessBuilder builder = new ProcessBuilder(arguments);
            Process p = builder.start();

            // 读取Python脚本的输出
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            StringBuilder readOut = new StringBuilder();
            String lastLine = "";
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                lastLine = line;
                readOut.append(line).append("  ");
            }
            in.close();
            // 等待Python脚本执行完成
            p.waitFor();
            // 获取退出值
            int exitValue = p.exitValue();
            log.info(pyFilePath + "运行完毕，退出值为：" + exitValue);
            if(exitValue == 0 || exitValue == 1) {
                result = "成功";
                FileUtil.copy(new File(name), result_path);
                log.info("生成频域瓜皮图" + result_path + "/" + name);
            } else {
                log.info("频域gltf生成失败" +pyFilePath+ "进程运行失败，命令详情："+arguments + "；标准输出流:"+readOut);
                return "频域gltf生成失败" + pyFilePath+ "进程运行失败；标准输出流:"+lastLine;
            }
        } catch (IOException e) {
            e.printStackTrace();
            result = "gltf生成失败";
        } catch (InterruptedException e) {
            e.printStackTrace();
            result = "gltf生成失败";
        }
        log.info("Python脚本执行完成");
        return result;
    }



    /**
     * 调用时域Python脚本并将结果文件复制到指定文件夹
     * @param pyFilePath "/root/tsbs/time_light/light_inp_to_gltf.py"
     * @param parameter json字符串
     * @param forToLight
     */
    public static String callBSPythonAndCopy(String pyFilePath, String parameter, String forToLight, String containerName) {
        String result = "";

        if(!FileUtil.isFileExist(pyFilePath)) {
            return pyFilePath+"不存在";
        }

        JSONObject json = JSON.parseObject(parameter);
        String inp_path = json.getString("inp_path");
        String result_path = json.getString("path");
        String name = "guapi.gltf";
        //转换成Python脚本对应参数 创建numpy数组的字符串表示
        String hor = json.getString("hor");
        String ver = json.getString("ver");
        String freq = json.getString("freq");
        String radius = json.getString("radius");
        String center =  json.getString("model_coo_center") ;
        String right =  json.getString("model_coo_right") ;
        String head = json.getString("model_coo_head") ;
        String nameLightPath = "light.gltf";

        if (!FileUtil.isFileExist(inp_path)) {
            return inp_path + "不存在";
        }
        FileUtil.copyFile(inp_path, "/root/tsbsDuiJie/time_light");//为方便查看，将inp文件复制到测试路径下

        //python3 ./time_light/light_inp_to_gltf.py  ./mesh20240401.inp light.gltf True
//        String[] arguments = new String[] {"python3", pyFilePath, inp_path, nameLightPath, forToLight };
        String directory = "/root/tsbsDuiJie";
//        String[] arguments = {"docker", "exec", "-w", directory, containerName, "python3", pyFilePath, inp_path, nameLightPath, forToLight};
        String[] arguments = {"podman", "exec", "-w", directory, containerName, "python3", pyFilePath, inp_path, nameLightPath, forToLight};
        try {
//            Process p = Runtime.getRuntime().exec(arguments);// 执行
            log.info("调用python脚本" + pyFilePath + "，命令行参数：" + Arrays.toString(arguments));
            ProcessBuilder builder = new ProcessBuilder(arguments);
            Process p = builder.start();
            StringBuilder readOut = new StringBuilder();
            String lastLine = "";
            // 读取Python脚本的输出
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                lastLine = line;
                readOut.append(line).append("  ");
            }
            in.close();
            // 等待Python脚本执行完成
            p.waitFor();
            // 获取退出值
            int exitValue = p.exitValue();
            log.info(pyFilePath + "运行完毕，退出值为：" + exitValue);
            if(exitValue == 0 || exitValue == 1) {
                //for_light=True用于亮点计算（时域）
                FileUtil.copy(new File(nameLightPath), result_path);
                log.info("生成时域瓜皮图" + result_path + "/" + nameLightPath);
                result = "成功";
            } else {
                log.info("时域gltf生成失败" +pyFilePath+ "进程运行失败，命令详情："+arguments + "；标准输出流:"+readOut);
                return "时域gltf生成失败" +pyFilePath+ "进程运行失败；标准输出流:"+lastLine;
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.info(pyFilePath+"脚本执行失败，gltf生成失败;"+e.getMessage());
            result = pyFilePath+"脚本执行失败，gltf生成失败";
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.info(pyFilePath+"脚本执行失败，gltf生成失败;"+e.getMessage());
            result = pyFilePath+"脚本执行失败，gltf生成失败";
        }
        return result;
    }


    /**
     * @param parentFolderPath  指定的父文件夹的绝对路径:basePath+id
     * @param fileFolderName    要创建的子文件夹名称:raw_cad或raw_mesh
     * @return
     */
    public static String creatFile(String parentFolderPath, String modelJsonMessage,
                                   String ImgName, MultipartFile imgFile, String fileFolderName, MultipartFile[] uploadFiles){
        String imgPath = "C:\\";

        //创建父文件夹（如果不存在）
        File parentFolder = new File(parentFolderPath);
        if (!parentFolder.exists()) {
            parentFolder.mkdirs();
        }

        //生成parameter.json文件到指定目录
        try {
            FileUtil.creatJsonFile(modelJsonMessage,"parameter.json");
        } catch (Exception e) {
            log.error("parameter.json文件保存失败！" + e.getMessage());
        }

        //将的图片数据写入到文件中
        if(!imgFile.isEmpty()) {
            File imageFile = ImgFileUtil.MultipartFileToFile(imgFile);
            imgPath = ImgFileUtil.saveToImgByBytes(imageFile, parentFolderPath, ImgName);
        }

        //创建的子文件夹,并将文件写入子文件夹中
        if(!fileFolderName.isEmpty()) {
            //创建的子文件夹
            String path = parentFolderPath + fileFolderName;
            File folder = new File(path);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            //文件写入子文件夹中
            FileUtil.createLocalFiles(path,uploadFiles);
        }

        return imgPath;
    }


}
